/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deadlocksim.gui;
import deadlocksim.*;
import deadlocksim.backend.resources.*;
import deadlocksim.backend.process.DeadlockProcess;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;

/**
 *
 * @author CCSCHMID
 */
public class ResourceUsageButton extends GradientButton
{
    private DeadlockSim mySim;
    //orignal declarations
    private int resourcesUsed;
    private int resourcesWanted;
    private Color RESOURCES_WANTED_COLOR1 = Color.orange;//GuiConfig.PURPLE_MED;
    private Color RESOURCES_WANTED_COLOR2 = Color.orange.darker();//GuiConfig.PURPLE_DARK;
    private Color RESOURCES_WANTED_COLOR3 = Color.orange.brighter();//GuiConfig.PURPLE_LIGHT;
    
    
    private Color NO_RESOURCES_WANTED_COLOR1 = GuiConfig.GREEN_MED;
    private Color NO_RESOURCES_WANTED_COLOR2 = GuiConfig.GREEN_DARK;
    private Color NO_RESOURCES_WANTED_COLOR3 = GuiConfig.GREEN_LIGHT;
    
    private Color NO_WANTED_NO_USED_COLOR1 = GuiConfig.GRAY_MED;
    private Color NO_WANTED_NO_USED_COLOR2 = GuiConfig.GRAY_DARK;
    private Color NO_WANTED_NO_USED_COLOR3 = GuiConfig.GRAY_LIGHT;
    DeadlockProcess usageP;
    int usageResourceID;
    
    //start popupmenu declarations
    private JPopupMenu usagePopupMenu;
    private JMenuItem freeUsageMenuItem;
    private JMenuItem freeAllUsageMenuItem;
    private JMenuItem cancelUsageMenuItem;
    private ActionListener usageActionListener;
    private MouseListener usagePopupListener;
    //end popupmenu declarations
    
   
    public ResourceUsageButton(DeadlockProcess p, int resourceID, DeadlockSim sim)
    {
        mySim = sim;
        usageP=p;
        usageResourceID=resourceID;
        initUsagePopupMenu();
        this.addMouseListener(usagePopupListener);
        refreshView();
    }
    public boolean equals(Object o)
    {
        if(o instanceof ResourceUsageButton)
        {
            ResourceUsageButton otherB = (ResourceUsageButton)o;
            if(otherB.usageResourceID == this.usageResourceID)
            {
                return true;
            }
            else
                return false;
        }
        else
        {
            return false;
        }
    }
    public void refreshView()
    {
        if(mySim == null)
            return;
        Integer numUsed = mySim.getResourceManager().getNumUsed(usageP, this.usageResourceID);
        Integer numWanted = mySim.getResourceManager().getNumWanted(usageP, this.usageResourceID);
        this.setInfo(numUsed, numWanted);
        this.setStatusColors();
        //this.setBackground(getDisplayColor());
        this.setText(getDisplayString());
    }
    
    public void setInfo(Integer numUsed, Integer numWanted)
    {
        if(numUsed == null || numWanted == null)
        {
            resourcesUsed = 0;
            resourcesWanted = 0;
            return;
        }
        resourcesUsed = numUsed;
        resourcesWanted = numWanted;
    }
    public void setResourcesWanted(int number)
    {
        resourcesWanted = number;
        // Then refresh display
    }
    
    public void setResourcesUsed(int number)
    {
        resourcesUsed = number;
    }
    public void setStatusColors()
    {
        
        if(resourcesWanted > 0)
        {
            this.setColors(RESOURCES_WANTED_COLOR1, RESOURCES_WANTED_COLOR2, RESOURCES_WANTED_COLOR3);
        }
        else if(resourcesWanted == 0 && resourcesUsed == 0)
        {
            this.setColors(this.NO_WANTED_NO_USED_COLOR1, this.NO_WANTED_NO_USED_COLOR2, this.NO_WANTED_NO_USED_COLOR3);
        }
        else
        {
            this.setColors(this.NO_RESOURCES_WANTED_COLOR1, this.NO_RESOURCES_WANTED_COLOR2, this.NO_RESOURCES_WANTED_COLOR3);
        }
    }
    public String getDisplayString()
    {
        return "" + resourcesUsed + " | " + -1*resourcesWanted;
    }
    
    //start popupmenu methods
    public void initUsagePopupMenu()
    {
        usagePopupMenu = new JPopupMenu("Usage Menu");
                
        String freeUsageDisplayString = "";
        if(usageP != null)
            freeUsageDisplayString = "Free 1 R"+usageResourceID+ 
                " resource from process P" + usageP.getID();
        freeUsageMenuItem = new JMenuItem(freeUsageDisplayString);
        freeUsageMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //perform free resource operation
                ResourceManager m = mySim.getResourceManager();
                if(m.getNumUsed(usageP, usageResourceID) > 0)
                {
                    m.takeResource(usageP, usageResourceID);
                    mySim.sendMessage("Freeing 1 R"+usageResourceID
                            + " resource from process P" + usageP.getID() +"!",false);
                    mySim.refreshDisplay();
                }
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        usagePopupMenu.add(freeUsageMenuItem);
        
        String freeAllUsageString = "";
        if(usageP != null)
            freeAllUsageString = "Free ALL R"+usageResourceID+ 
                " resources from process P" + usageP.getID();
        freeAllUsageMenuItem = new JMenuItem(freeAllUsageString);
        freeAllUsageMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //perform delete usage operation
                mySim.getResourceManager().takeAllOfType(usageP, usageResourceID);
                mySim.refreshDisplay();
                if(mySim.isRunning())
                    mySim.startClock();
            }
        });
        usagePopupMenu.add(freeAllUsageMenuItem);        
        
        cancelUsageMenuItem = new JMenuItem("Cancel");
        cancelUsageMenuItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                //cancel the menu, nothing to do
                //mySim.sendmessage("Cancel");
                if(mySim.isRunning())
                    mySim.startClock();
                
            }
        });
        usagePopupMenu.add(cancelUsageMenuItem);
        
        usagePopupListener = new PopupListener(usagePopupMenu);
    }
    
    class PopupListener extends MouseAdapter {
        JPopupMenu popup;
        
        PopupListener(JPopupMenu popupMenu) {
            popup = popupMenu;
        }
        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }
        private void maybeShowPopup(MouseEvent e) {
            if (!e.isPopupTrigger()) {
                if(mySim.isRunning())
                    mySim.stopClock();
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }
}
